<?php
/**
 * Holds class ArrayList
 *
 * @author daantimmer
 *
 * @package Library
 */

/**
 * Custome made array list. Can bi iterated using foreach
 *
 * @see IteratorAggregate
 * @author daantimmer
 */
class ArrayList implements IteratorAggregate{
    
	/**
	 * The array that holds the values
	 *
	 * @var mixed
	 */
	private $items = array();
	
	/**
	 * Internal counter
	 *
	 * @var integer
	 */
    private $count = 0;
	
	/**
	 * Returns the iterator used for foreach
	 *
	 * @return MyIterator
	 */
	public function getIterator() {
        return new MyIterator(array_values($this->items));
    }

    /**
     * Adds a value to the list
     *
     * @param mixed $value
     * @return void
     */
    public function add($value) {
        $this->items[$this->count++] = $value;
    }
    
    /**
     * Check if $value exists
     *
     * Uses the builtin function in_array to look for $value.
     *
     * @param mixed $value
     * @return boolean
     */
    public function exists($value){
    	return in_array($value, $this->items, true);
    }
    
    /**
     * Returns current size of the list
     *
     * @return integer
     */
    public function size(){
    	return count($this->items);
    }
    
    /**
     * Removes $value from the list
     *
     * @param mixed $value
     * @return boolean
     */
    public function remove($value){
    	if(!in_array($value, $this->items,true))
    		return false;
    	
    	unset($this->items[array_search($value,$this->items, true)]);
    	
    	if(!in_array($value, $this->items,true))
    		return true;
    	return false;
    }
    
    /**
     * Checks if the list is empty
     *
     * @return boolean
     */
    public function isEmpty(){
    	return $this->size() == 0;
    }
    
    /**
     * Get the first item in the list
     *
     * Uses a mixture of array_keys and array_shift to get the first key from the list.
     *
     * @return mixed
     */
    public function getFirst(){
    	$keys = array_keys($this->items);
    	$key = array_shift($keys);
    	return $this->items[$key];
    }
}